/*********************************************************
*
*  Project:      Utilities
*
*  Class:        ExtractSimplifiedRadicals
*
*  File name:    ExtractSimplifiedRadicals.java
*
*
*    SVN:
*
*    $Rev$:
*    $Date$:
*
*    $LastChangedBy$:
*
*    $HeadURL$:
*
**********************************************************/

package com.eastmountain.chinese.radicals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class ExtractSimplifiedRadicals {

    /***********************************************************************************************
     *
     * readInput
     *
     **********************************************************************************************/

    private static void readInput( File inputFile ) {

        String line = "didn't get a file.";
        String correctedLine;
        File shortenedFile = null;
        int k;

        try {

            FileInputStream fis = new FileInputStream( inputFile );
            InputStreamReader isr = new InputStreamReader( fis, "UTF-8" );
            BufferedReader in = new BufferedReader( isr );

            shortenedFile = new File( "simplified-radicals.txt" );
            // FileOutputStream FileOutputStream = new FileOutputStream( shortenedFile );
            PrintWriter shortenedFileWriter = new PrintWriter( shortenedFile, "UTF-8" );

            System.out.println( "\n    ******   readInput(): " + inputFile.getName() + "\n" );

            //  Extract the simplified radicals from the Wiktionary
            //  on Chinese radicals

            k = 0;

            while ( (line = in.readLine()) != null ) {

                System.out.println( "^^^" + line );

                if ( line.contains( "or simplified as" ) )  {

                    k++;

                    shortenedFileWriter.println( line );

                }

            }

            System.out.println( "\nRecords written: " + k );

            in.close();
            shortenedFileWriter.close();

        } catch ( FileNotFoundException fnfe ) {

            System.out.println( "Cannot locate input file! " + fnfe.getMessage() );
            System.exit( 12 );

        } catch ( IOException exception ) {

            System.out.println( "Unexpected Exception:  " + exception.getMessage() );
            System.exit( 12 );

        }

    }

    /*************************************************
     *
     *   main
     *
     ************************************************/

    public static void main( String[] args ) {

        File radicalFile = new File( "Index Chinese_radical.htm.txt" );

        readInput( radicalFile );

    }
}
